<template>
  <div class="demo18">
    <div>
      {{num}}
      <div>
        <Button @click="resetBtnClick">清零</Button>
        <Button @click="handleParse">暂停</Button>
        <Button @click="handleAdd">继续/开始</Button>
      </div>
    </div>
  </div>

</template>

<script>
  export default {
    name: 'demo18',
    components: {},
    props: {},
    data() {
      return {
        num: 0,
        timer: null
      };
    },
    mounted() {
    },
    methods: {
      handleAdd() {

        // 如果定时器已经存在 说明现在正在倒计时中  所以直接返回
        if (this.timer) return;
        // 开启定时器   每秒数字加1
        this.timer = setInterval(() => {
          this.num++
        }, 1000)


      },

      // 点击暂停按钮
      handleParse() {
        // 清空定时器
        clearInterval(this.timer)
        this.timer = null
      },
      // 点击清零按钮
      resetBtnClick() {
        this.handleParse()
        this.num = 0
      }
    },
    computed: {}
  };
</script>

<style scoped lang="less">
  .demo18 {
  }
</style>
<style lang="less">
  .demo18 {
  }
</style>
